package org.jf.tool.fx.jftoolfx.tools.cron.controller;

import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Spinner;
import javafx.scene.control.ToggleGroup;

import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

/**
 * cron 工具
 *
 * @author jifeng_zhang
 * @date 2025/10/9 上午11:41
 */
public class MinuteController extends CronController {

    @FXML
    private ToggleGroup typeToggleGroup;
    @FXML
    private Spinner<Integer> periodBegin;
    @FXML
    private Spinner<Integer> periodEnd;
    @FXML
    private Spinner<Integer> startBegin;
    @FXML
    private Spinner<Integer> startEnd;
    @FXML
    private CheckBox checkBox0;
    @FXML
    private CheckBox checkBox1;
    @FXML
    private CheckBox checkBox2;
    @FXML
    private CheckBox checkBox3;
    @FXML
    private CheckBox checkBox4;
    @FXML
    private CheckBox checkBox5;
    @FXML
    private CheckBox checkBox6;
    @FXML
    private CheckBox checkBox7;
    @FXML
    private CheckBox checkBox8;
    @FXML
    private CheckBox checkBox9;
    @FXML
    private CheckBox checkBox10;
    @FXML
    private CheckBox checkBox11;
    @FXML
    private CheckBox checkBox12;
    @FXML
    private CheckBox checkBox13;
    @FXML
    private CheckBox checkBox14;
    @FXML
    private CheckBox checkBox15;
    @FXML
    private CheckBox checkBox16;
    @FXML
    private CheckBox checkBox17;
    @FXML
    private CheckBox checkBox18;
    @FXML
    private CheckBox checkBox19;
    @FXML
    private CheckBox checkBox20;
    @FXML
    private CheckBox checkBox21;
    @FXML
    private CheckBox checkBox22;
    @FXML
    private CheckBox checkBox23;
    @FXML
    private CheckBox checkBox24;
    @FXML
    private CheckBox checkBox25;
    @FXML
    private CheckBox checkBox26;
    @FXML
    private CheckBox checkBox27;
    @FXML
    private CheckBox checkBox28;
    @FXML
    private CheckBox checkBox29;
    @FXML
    private CheckBox checkBox30;
    @FXML
    private CheckBox checkBox31;
    @FXML
    private CheckBox checkBox32;
    @FXML
    private CheckBox checkBox33;
    @FXML
    private CheckBox checkBox34;
    @FXML
    private CheckBox checkBox35;
    @FXML
    private CheckBox checkBox36;
    @FXML
    private CheckBox checkBox37;
    @FXML
    private CheckBox checkBox38;
    @FXML
    private CheckBox checkBox39;
    @FXML
    private CheckBox checkBox40;
    @FXML
    private CheckBox checkBox41;
    @FXML
    private CheckBox checkBox42;
    @FXML
    private CheckBox checkBox43;
    @FXML
    private CheckBox checkBox44;
    @FXML
    private CheckBox checkBox45;
    @FXML
    private CheckBox checkBox46;
    @FXML
    private CheckBox checkBox47;
    @FXML
    private CheckBox checkBox48;
    @FXML
    private CheckBox checkBox49;
    @FXML
    private CheckBox checkBox50;
    @FXML
    private CheckBox checkBox51;
    @FXML
    private CheckBox checkBox52;
    @FXML
    private CheckBox checkBox53;
    @FXML
    private CheckBox checkBox54;
    @FXML
    private CheckBox checkBox55;
    @FXML
    private CheckBox checkBox56;
    @FXML
    private CheckBox checkBox57;
    @FXML
    private CheckBox checkBox58;
    @FXML
    private CheckBox checkBox59;

    private List<CheckBox> allCheckBoxes;
    private CronController cronController;

    @Override
    protected void init(URL url, ResourceBundle resourceBundle) {
        allCheckBoxes = Arrays.asList(checkBox0, checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10, checkBox11, checkBox12, checkBox13, checkBox14, checkBox15, checkBox16, checkBox17, checkBox18, checkBox19, checkBox20, checkBox21, checkBox22, checkBox23, checkBox24, checkBox25, checkBox26, checkBox27, checkBox28, checkBox29, checkBox30, checkBox31, checkBox32, checkBox33, checkBox34, checkBox35, checkBox36, checkBox37, checkBox38, checkBox39, checkBox40, checkBox41, checkBox42, checkBox43, checkBox44, checkBox45, checkBox46, checkBox47, checkBox48, checkBox49, checkBox50, checkBox51, checkBox52, checkBox53, checkBox54, checkBox55, checkBox56, checkBox57, checkBox58, checkBox59);
        // 添加值改变监听器（推荐方式）
        periodBegin.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue >= periodEnd.getValue()) {
                periodBegin.getValueFactory().setValue(periodEnd.getValue() - 1);
            }
            typeCheck();
        });
        periodEnd.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue <= periodBegin.getValue()) {
                periodEnd.getValueFactory().setValue(periodBegin.getValue() + 1);
            }
            typeCheck();
        });
        startBegin.valueProperty().addListener((observable, oldValue, newValue) -> {
            typeCheck();
        });
        startEnd.valueProperty().addListener((observable, oldValue, newValue) -> {
            typeCheck();
        });
    }

    public void setCronController(CronController cronController) {
        this.cronController = cronController;
    }

    public void typeCheck() {
        String type = typeToggleGroup.getSelectedToggle().getUserData().toString();
        cronController.cronMinuteText.setText("*");
        switch (type) {
            case "second":
                break;
            case "period":
                int periodBeginValue = periodBegin.getValue();
                int periodEndValue = periodEnd.getValue();
                cronController.cronMinuteText.setText(periodBeginValue + "-" + periodEndValue);
                break;
            case "start":
                int startBeginValue = startBegin.getValue();
                int startEndValue = startEnd.getValue();
                cronController.cronMinuteText.setText(startBeginValue + "/" + startEndValue);
                break;
            case "select":
                StringBuilder selectText = new StringBuilder();
                for (int i = 0; i < allCheckBoxes.size(); i++) {
                    if (allCheckBoxes.get(i).isSelected()) {
                        selectText.append(i).append(",");
                    }
                }
                if (!selectText.isEmpty()) {
                    selectText = selectText.deleteCharAt(selectText.length() - 1);
                } else {
                    selectText.append("*");
                }
                cronController.cronMinuteText.setText(selectText.toString());
                break;
        }
        cronController.cronText.setText(cronController.cronSecondText.getText() + " " +
                cronController.cronMinuteText.getText() + " " +
                cronController.cronHourText.getText() + " " +
                cronController.cronDayText.getText() + " " +
                cronController.cronMonthText.getText() + " " +
                cronController.cronWeekText.getText() + " " +
                cronController.cronYearText.getText());
        cronController.resultText.setText("");
    }
}
